package com.yonyou.iuap.jxzy.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.yonyou.iuap.jxzy.servlet.dao.DeviceMapper;

public class AutoReadCardServlet extends HttpServlet {
	
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	private static final long serialVersionUID = 1L;
	
	@Autowired
	private DeviceMapper deviceMapper;
	
	ScheduledExecutorService scheduledThreadPool = null;
	
	private DeviceInfo curdev;
	
	@Override
	public void init() throws ServletException {
		//创建一个工作任务每2s执行一次
		if(scheduledThreadPool == null){
		//通过springBean来对本身进行注解 Autowired注入
			SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this,getServletContext());
			List<DeviceInfo> dev_list = this.deviceMapper.queryDeviceInfo();
			if(dev_list == null || dev_list.size() == 0){
				logger.error("需要监听的制动读卡设备数量为0初始化结束！！");
				return;
			}
			//创建核心线程数，更具返回的设备数量构建核心线程---3个硬件一个核心线程
//			scheduledThreadPool = Executors.newScheduledThreadPool(dev_list.size());
			for(DeviceInfo dev : dev_list){
				//注入一个定时线程2秒执行一次
				curdev=dev;
				logger.error("自动读卡设备初始化:"+dev.getDeviceCode()+"_"+dev.getDeviceName());
//				if("10040".equals(dev.getDeviceCode()) || "10041".equals(dev.getDeviceCode())){
					if(dev.getVdef2()!=null){//正式改为 is null
//						AutoReadSokect autoReadSokect;
						try {
							ExecutorService newCachedThreadPool = Executors.newCachedThreadPool();
							newCachedThreadPool.execute(new Runnable() {
								@Override
								public void run() {
									int i=0;
									DeviceInfo restardev=coloneDeviceInfot(curdev);
									logger.error("自动读卡设备初始化:"+restardev.getDeviceCode()+"_"+restardev.getDeviceName());
									try {
										AutoReadSokect2 autoReadSokect = new AutoReadSokect2(restardev);
										autoReadSokect.AutoRead();
									}catch (Exception e) {
										i=1;
										e.printStackTrace();
										logger.error(e.getMessage());
									}
									while(i==1){
										try {
											logger.error("自动读卡设备进入重新初始化:"+restardev.getDeviceCode()+"_"+restardev.getDeviceName());
											AutoReadSokect2 autoReadSokect = new AutoReadSokect2(restardev);
											autoReadSokect.AutoRead();
											i=0;
										}catch (Exception e) {
											i=1;
											e.printStackTrace();
											logger.error(e.getMessage());
										}
									}
								}
							});
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
							logger.error(e.getMessage());
						}
						
					}
//					scheduledThreadPool.scheduleWithFixedDelay(new AutoReadThread(dev), 2, 2, TimeUnit.SECONDS);
//				}
				try {
					Thread.sleep(10000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					logger.error(e.getMessage());
				}
			}
		}
		
	}
	
	public DeviceInfo coloneDeviceInfot(DeviceInfo d){
		DeviceInfo deviceInfo = new DeviceInfo();
		deviceInfo.setDeviceCode(d.getDeviceCode());
		deviceInfo.setDeviceName(d.getDeviceName());
		deviceInfo.setClassName(d.getClassName());
		deviceInfo.setExtendClass(d.getExtendClass());
		deviceInfo.setAutosend(d.getAutosend());
		deviceInfo.setUserData(d.getUserData());
		deviceInfo.setVdef1(d.getVdef1());
		deviceInfo.setVdef2(d.getVdef2());
		return deviceInfo;
	}
	
	@Override
	public void destroy() {
		super.destroy();
		if(scheduledThreadPool!=null){
			logger.info("销毁scheduledThreadPool");
			scheduledThreadPool.shutdown();
		}
	}
	
}
